
let mobile = document.querySelector('input[type=text]');
let pwd1 = document.querySelector('.pwd1');
let pwd2 = document.querySelector('.pwd2');
let sendText = document.querySelector('.sendText');
let sendDom = document.querySelector('.send');
let btnDom = document.querySelector('.btn');
let titleDom = document.querySelector('.title'); //修改成功的提示信息 
let verifyCodeDom = document.querySelector('.verifyCode');
let flag1 = flag2 = flag3 = flag4 = flag5 = false;
mobile.onblur = function () {
    // console.log(mobile.value);    
    //手机号是否符合要求    
    if (/^1[356789]\d{9}$/.test(mobile.value)) {
        this.nextElementSibling.innerHTML = `<span style = "color:green">√</span>`;
        sendDom.disabled = false
        flag1 = true;
    } else {
        this.nextElementSibling.innerHTML = `<span style = "color:red;font-size:12px;">格式有误</span>`;
        flag1 = false;
        sendDom.disabled = true
    }
    changeRe()
}
//密码是否符合要求
//密码框1
pwd1.onblur = () => {
    if (pwd1.value.length >= 6) {
        pwd1.nextElementSibling.innerHTML = `<span style = "color:green">√</span>`;
        flag2 = true
    } else {
        pwd1.nextElementSibling.innerHTML = `<span style = "color:red;font-size:12px;">格式有误</span>`;
        flag2 = false
    }
    changeRe()
}

//密码框2
pwd2.onblur = () => {
    if (pwd2.value.length >= 6) {
        pwd2.nextElementSibling.innerHTML = `<span style = "color:green">√</span>`;
        flag3 = true
    } else {
        pwd2.nextElementSibling.innerHTML = `<span style = "color:red ;font-size:12px;">格式有误</span>`;
        flag3 = false
    }
    changeRe()
    // 两个密码框密码需一致且不能为空
    if ((pwd1.value == pwd2.value) && (pwd1.value != "" && pwd2.value != "")) {
        pwd2.nextElementSibling.innerHTML = `<span style = "color:green">√</span>`;
        flag4 = true
    } else {
        pwd2.nextElementSibling.innerHTML = `<span style = "color:red ;font-size:12px;">两次密码不一致</span>`;
        flag4 = false
    }
    changeRe()
}



//判断输入的值是否符合要求
function changeRe() {
    var btn = document.querySelector('.btn');
    (flag1 && flag2 && flag3 && flag4 && flag5) ? (btn.disabled = false) : (btn.disabled = true);
}

//倒计时初始值
let time = 30;
//保存定时器标识
let timer;  //验证码定时器



//点击发送验证码
sendDom.onclick =async () => {
    let response = await axios({
        method: 'post',
        url: 'http://39.98.112.34:8000/base/user/sendVerify',
        data: {mobile:mobile.value,scenic:'2'},
    })
    let resData = response.data.data
    console.log(resData);
    timer = setInterval(() => {
        time--;
        if (time > 0) {
            sendDom.disabled = true;
            sendDom.parentElement.lastElementChild.innerHTML = `${time}s后重新获取`;
            verifyCodeDom.innerHTML = resData.verifyCode;
        } else {
            sendDom.disabled = false;
            sendDom.parentElement.lastElementChild.innerHTML = `重新获取`;
            time = 30;
            clearInterval(timer);
        }
    }, 1000);

    //验证码框
    sendText.onblur = () => {
        if (sendText.value == resData.verifyCode) {
            flag5 = true;
        } else {
            sendText.setAttribute('placeholder', '验证码有误!!!');
            flag5 = false;
        }
        changeRe();
    }

}


// //修改
btnDom.onclick =  () => {
    // 发送ajax请求
     ajax({
        url: 'http://39.98.112.34:8000/base/user/resetpwd',
        type: 'post',
        data: { verifyCode: verifyCodeDom.innerHTML, mobile: mobile.value, password: pwd1.value },
    })
    //重置输入框所有的值
    mobile.value = '';
    pwd1.value = '';
    pwd2.value = '';
    mobile.nextElementSibling.innerHTML = '';
    pwd1.nextElementSibling.innerHTML = '';
    pwd2.nextElementSibling.innerHTML = '';
    verifyCodeDom.innerHTML = '';
    sendDom.parentElement.lastElementChild.innerHTML = `发送验证码`;
    titleDom.innerHTML = `密码修改成功！！！`;
    btnDom.disabled = true;
}


